package com.wawayaya.bookreader.bean;

import android.graphics.Color;
import android.text.TextUtils;

import com.wawayaya.bookreader.DefaultSettings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


// all book pages' data, recognized by innerpage number
public class Pagesdata implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private int m_ipagedefwidth = 0;
	private int m_ipagedefheight = 0;
	private int m_ibooktotalpagecount = 0;
	private int m_ibackcovercount = 0;
	private int m_icontentpagecount = 0;
	private Pagedata[] m_pd = null;
	// inner-page to bookpagesummary object; inner-page starts from 0
	private Map<Integer, Bookpagesummary> m_mapinnerpage2summary = Collections.synchronizedMap(new HashMap<Integer, Bookpagesummary>());
	 // page-name to bookpagesummary object;
	private Map<String, Bookpagesummary> m_mapname2summary = Collections.synchronizedMap(new HashMap<String, Bookpagesummary>());
	 // page-number to inner-page; page-number must equal to or larger than 1
	private Map<Integer, Integer> m_mappagenumber2innerpage = Collections.synchronizedMap(new HashMap<Integer, Integer>());
	private int m_ibegininnerpage = -1;
	private int m_iendinnerpage = -1;
	private String[] m_ssquizurls = null;
	private boolean m_bcalculated = false;
	private String m_sserializedpath = null;
	//private int m_iimgcombineType = ConstValues.ms_Json_ImgCombineType_unknown;
	private int       m_kokColor = Color.RED;
    private boolean   m_isLandScapeBook = false;

    public Pagesdata(){}


    public Pagesdata(ArrayList<Bookpagesummary> albps, int ipartnumber, int ipagedefwidth, int ipagedefheight, int kok_cl, boolean landscape) {
        if(null == albps) {
            return;
        }
        m_kokColor = kok_cl;
        m_ipagedefwidth = ipagedefwidth;
        m_ipagedefheight = ipagedefheight;
        m_ibooktotalpagecount = albps.size();
        m_isLandScapeBook = landscape;


        int ipartpages = 0;
        int ipagenumber = 1;
        for(int i = 0; i < albps.size(); i++) {
			Bookpagesummary bps = albps.get(i);
            if(null != bps) {
                bps.m_iinnerpage = i;
                if(DefaultSettings.ms_Json_BookPageType_page == bps.m_itype) {
                    bps.m_ipagenumber = ipagenumber;
                    ipagenumber++;
                    m_icontentpagecount++;
                }
                else if(DefaultSettings.ms_Json_BookPageType_backover == bps.m_itype) {
                    m_ibackcovercount++;
                }
                if(bps.m_ipartnumber == ipartnumber) {
                    if(m_ibegininnerpage < 0) {
                        m_ibegininnerpage = i;
                    }
                    m_iendinnerpage = i;
                    ipartpages++;
                }
                m_mapinnerpage2summary.put(bps.m_iinnerpage, bps);
                m_mapname2summary.put(bps.m_name, bps);
                if(bps.m_ipagenumber >= 1) {
                    m_mappagenumber2innerpage.put(bps.m_ipagenumber, bps.m_iinnerpage);
                }
            }
        }
        m_pd = new Pagedata[ipartpages];
    }

/*	public pagesdata(ArrayList<bookpagesummary> albps, int ipartnumber, int ipagedefwidth, int ipagedefheight, int kok_cl, boolean landscape) {
		if(null == albps) {
			return;
		}
        MyLog.dee("TrdBookPartParse : pagesdata 6666666666666666   ipartnumber: "+ipartnumber);
		m_kokColor = kok_cl;
		m_ipagedefwidth = ipagedefwidth;
		m_ipagedefheight = ipagedefheight;
		m_ibooktotalpagecount = albps.size();
        m_isLandScapeBook = landscape;


		int ipartpages = 0; 
		int ipagenumber = 1;
		for(int i = 0; i < albps.size(); i++) {
            MyLog.dee("TrdBookPartParse : pagesdata for  77777777777777777777    ipartnumber: "+ipartnumber);
			bookpagesummary bps = albps.get(i);
			if(null != bps) {
				bps.m_iinnerpage = i;
				if(ConstValues.ms_Json_BookPageType_page == bps.m_itype) {
					bps.m_ipagenumber = ipagenumber;
					ipagenumber++;
					m_icontentpagecount++;
				}
				else if(ConstValues.ms_Json_BookPageType_backover == bps.m_itype) {
					m_ibackcovercount++;
				}
				if(bps.m_ipartnumber == ipartnumber) {
					if(m_ibegininnerpage < 0) {
						m_ibegininnerpage = i;
					}
					m_iendinnerpage = i;
					ipartpages++;
				}
				m_mapinnerpage2summary.put(bps.m_iinnerpage, bps);
				m_mapname2summary.put(bps.m_name, bps);
				if(bps.m_ipagenumber >= 1) {
					m_mappagenumber2innerpage.put(bps.m_ipagenumber, bps.m_iinnerpage);
				}
			}
		}
		m_pd = new pagedata[ipartpages];
	}
	*/
	public void clear() {
		m_ibooktotalpagecount = 0;
		m_ibackcovercount = 0;
		m_icontentpagecount = 0;
		m_pd = null; 
		m_mapinnerpage2summary.clear();
		m_mapname2summary.clear();
		m_mappagenumber2innerpage.clear();
		m_ibegininnerpage = -1;
		m_iendinnerpage = -1;
		m_ssquizurls = null;
		m_bcalculated = false;
		m_sserializedpath = null;
	}
	
	public IntDouble getbookdefwidthheight() {
		return new IntDouble(m_ipagedefwidth, m_ipagedefheight);
	}
	
	public String getserializedFilepath() {
		return m_sserializedpath;
	}
	
	public int getbooktotalpagescount() {
		return m_ibooktotalpagecount;
	}
	
	public int getbackcoverpagecount() {
		return m_ibackcovercount;
	}
	
	public int getcontentpagecount() {
		return m_icontentpagecount;
	}
	
	public boolean hasquiz() {
		return (null != m_ssquizurls && m_ssquizurls.length > 0 && !TextUtils.isEmpty(m_ssquizurls[0]));
	}
	
	public IntDouble getapplicationscreensize(int iinnerpage) {
		int ipos = convertinnerpage(iinnerpage, true);		
		return m_pd[ipos].getapplictionscreensize();
	}

	public IntDouble getbackgroundimgsize(int iinnerpage){ 
		int ipos = convertinnerpage(iinnerpage, true);		 
		return m_pd[ipos].getbackgroundimgsize();
	}

	public IntDouble getbookpagebackgroundimgsize(){
		if(null != m_pd){
			for(Pagedata pd : m_pd) {
				if(null != pd) {
					if(DefaultSettings.ms_Json_BookPageType_page == pd.getpageType()) {
						return pd.getbackgroundimgsize();
					}
				}
			}
		}
		return null;
	}
	
	public IntDouble getforegroundimgsize(int iinnerpage){
		int ipos = convertinnerpage(iinnerpage, true);		  
		return m_pd[ipos].getforegroundimgsize();
	}
	
	public String[] getquizurls() {
		return m_ssquizurls;
	}
	
	public int getpagetype(int iinnerpage) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0  && null != m_pd && null != m_pd[ipos]){
			return m_pd[ipos].getpageType();
		}
		return DefaultSettings.ms_Json_BookPageType_unknown;
	}
	
	public Pagedata getpagedata(int iinnerpage) {
		try{
			int ipos = convertinnerpage(iinnerpage, false);
			return m_pd[ipos];
		}catch(Exception e){
		}
		return null;
	}

	public boolean pageisin(int iinnerpage) {
		return (iinnerpage >= m_ibegininnerpage) && (iinnerpage <= m_iendinnerpage);
	}
	
	public int getbegininnerpageidx() {
		return m_ibegininnerpage;
	}
	
	public int getendinnerpageidx() {
		return m_iendinnerpage;
	}
	
	public int getpossiblepagenumber(int iinnerpage) {
		Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
		if(null != bps) {
			return bps.m_ipagenumber;
		}
		return -1;
	}

    public int getpossiblepageindex(int iinnerpage) {
		Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
        if(null != bps) {
            return bps.m_index;
        }
        return -1;
    }

    public int getpossiblepagetype(int iinnerpage) {
        Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
        if(null != bps) {
            return bps.m_itype;
        }
        return -1;
    }
	
	public Bookpagesummary getBookPageSummary(int iinnerpage) {
		Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
		return bps;
	}
	
	public int getpartindx(int iinnerpage){
		Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
		if(null != bps) {
			return bps.m_ipartnumber;
		}
		return -1;		
	}	
	
	public String getname(int iinnerpage) {
		Bookpagesummary bps = m_mapinnerpage2summary.get(iinnerpage);
		if(null != bps) {
			return bps.m_name;
		}
		return null;
	}
	
	public void setserializedFilepath(String sserializedFilepath) {
		m_sserializedpath = sserializedFilepath;
	}
	
	public void setquizurls(String[] ssquizurls) {
		m_ssquizurls = ssquizurls;
	}
	
	public void setpagetype(int iinnerpage, int itype) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].settype(itype);
		}
	}
	
	public void updatebackgroundCN(int iinnerpage, String sfullpath, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updatebackgroundCN(sfullpath, w, h);
		}
	}
	
	public void updatebackgroundCNPY(int iinnerpage, String sfullpath, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updatebackgroundCNPY(sfullpath, w, h);
		}
	}
	
	public void updatebackgroundCNEN(int iinnerpage, String sfullpath, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updatebackgroundCNEN(sfullpath, w, h);
		}
	}
	
	public void updatebackgroundCNPYEN(int iinnerpage, String sfullpath, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updatebackgroundCNPYEN(sfullpath, w, h);
		}
	}
	
	public void updateforegroundCN_2(int iinnerpage, String sfullpath, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updateforegroundCN_2(sfullpath, w, h);
		}
	}
	
	public void addbackgrounds_1(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].addbackgrounds_1(itotalcount, iidx, sfullpath, ileft, itop, w, h);
		}
	}

	public void addbackgroundCNs_3(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			//if (ReaderApplication.USE_NEW_KOK) {
				m_pd[ipos].addbackgroundCNs_3_new(itotalcount, iidx, sfullpath, ileft, itop, w, h);
			//}
			
			m_pd[ipos].addbackgroundCNs_3(itotalcount, iidx, sfullpath, ileft, itop, w, h);
			
		}
	}
	
	public void addforeKokCNs_3(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].addforeKoKCNs_3(itotalcount, iidx, sfullpath, ileft, itop, w, h);
        }
    }

    public void addforeKokENs_3(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].addforeKoKENs_3(itotalcount, iidx, sfullpath, ileft, itop, w, h);
        }
    }

	
	public void addbackgroundPYs_3(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h){
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].addbackgroundPYs_3(itotalcount, iidx, sfullpath, ileft, itop, w, h);
		}
	}
	
	public void addbackgroundENs_3(int iinnerpage, int itotalcount, int iidx, String sfullpath, int ileft, int itop, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}

            m_pd[ipos].addbackgroundEns_3_new(itotalcount, iidx, sfullpath, ileft, itop, w, h);

			m_pd[ipos].addbackgroundENs_3(itotalcount, iidx, sfullpath, ileft, itop, w, h);
		}
	}
	
	public void updatesentencetext(int iinnerpage, int isentenceid, String stext) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updatesentencetext(stext);
		}
	}
	
	public void setapplictionscreensize_1(int iinnerpage, int w, int h) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].setapplictionscreensize_1(w, h);
		}
	}
	
	public void updatesentencetrectlines_3(int iinnerpage, int isentenceid, StringDouble[] sds, int iscreenwidth, int iscreenheight) {
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(iscreenwidth < 200 || iscreenheight < 200) {
                m_bcalculated = false;
            }
            else{
                m_bcalculated = true;
            }
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }

            m_pd[ipos].updatesentencetrectlines_3_new(isentenceid, sds, iscreenwidth, iscreenheight, m_bcalculated);

        }
    }

    public void updateHotRegionData(int iinnerpage, ArrayList<HotRegionData> list) {
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].updateHotRegionData(list);
        }
    }

	
	public void updatesentencetcutword_4(int iinnerpage, int isentenceid, StringDouble[] sds) {
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].updatesentencetcutword_4(isentenceid, sds);
        }

	}
	
	public void updateaudiopathtime_5(int iinnerpage, int isentenceid, String smp3, StringIntDouble[] sids){
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].updateaudiopathtime_5(isentenceid, smp3, sids);
		}
	}
	
	public boolean positioniscalculated(){
		return m_bcalculated;
	}
	
	public void updatecharacterposition(int isreenwidth, int iscreenheight) {
		if(!m_bcalculated && isreenwidth > 200 && iscreenheight > 200) {
			if(null != m_pd) {
				for(Pagedata pd : m_pd) {
					if(null != pd) {
						if(!(m_bcalculated = pd.updatecharacterposition(isreenwidth, iscreenheight))) {
							break;
						}
					}
				}
			}
		}
	}	

	private int convertinnerpage(int innerpage, boolean bforcemaptome) {
		if(pageisin(innerpage)) {
			return innerpage - m_ibegininnerpage;
		}
		else if(bforcemaptome) {
			return m_ibegininnerpage;
		}
		return -1;
	}
	
	public int getKokColor(){
		return m_kokColor;
	}
	
	public void setTitleMp3(int iinnerpage, String title_mp3) {
		int ipos = convertinnerpage(iinnerpage, false);
		if(ipos >= 0) {
			if(null == m_pd[ipos]) {
				m_pd[ipos] = new Pagedata();
			}
			m_pd[ipos].setTitleMp3(title_mp3);
		}
	}

    public void setTrans(int iinnerpage, String trans) {
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].setTrans(trans);
        }
    }

    public void setEnglishPage(int iinnerpage, boolean englishpage) {
        int ipos = convertinnerpage(iinnerpage, false);
        if(ipos >= 0) {
            if(null == m_pd[ipos]) {
                m_pd[ipos] = new Pagedata();
            }
            m_pd[ipos].setEnglishPage(englishpage);
        }
    }

    public boolean isLandScapeBook() {
        return m_isLandScapeBook;
    }

}
